package com.inspur_video.inspur_video.aliyun;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-07-29 22:03
 * @package com.inspur_video.inspur_video.aliyun
 * @description
 */

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;
public class ALiYunUtil {
    private final static DefaultAcsClient client;


    private final static String accessKeyId="LTAIJVN0829GRf8J";


    private final static String accessKeySecret="URec2tnquQELlUMftlcvyGLF4Ksk2X";

    static {
        client= initVodClient();
    }
    /*初始化客户端*/
    private static DefaultAcsClient initVodClient() {
        //点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(String aId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(aId);
        request.setFormats("mp4");
        return client.getAcsResponse(request);
    }

    /**
     * 获取播放地址函数
     *
     * @param definition 自定义清晰度，FD流畅，LD标清
     * @param aId
     * @return
     * @throws Exception
     */

    public static GetPlayInfoResponse getPlayByDefinitionAndId(String definition, String aId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(aId);
        if (definition!=null){
            request.setDefinition(definition);
        }
        request.setFormats("mp4");
        return client.getAcsResponse(request);
    }

    /*获取播放凭证函数*/
    public static GetVideoPlayAuthResponse getVideoPlayAuth(String aId) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(aId);
        request.setAuthInfoTimeout((long) 7200);
        return client.getAcsResponse(request);
    }

    /*获取视频信息函数*/
    public static GetVideoInfoResponse getVideoInfo(String aId) throws Exception {
        GetVideoInfoRequest request = new GetVideoInfoRequest();
        request.setVideoId(aId);
        return client.getAcsResponse(request);
    }

    /*根据Date时间生成UTC时间函数*/
    private static String generateUTCTime(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        dateFormat.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
        dateFormat.setLenient(false);
        return dateFormat.format(time);
    }


    /*获取视频列表函数*/
    public static GetVideoListResponse getVideoList(String status) throws Exception {
        GetVideoListRequest request = new GetVideoListRequest();
        //new Date(System.currentTimeMillis() - 30 * 86400L)
        String monthAgoUTCTime = generateUTCTime(new Date(0));
        String nowUTCTime = generateUTCTime(new Date(System.currentTimeMillis()));
        // 视频创建的起始时间，为UTC格式
        request.setStartTime(monthAgoUTCTime);
        request.setSortBy("title");
        // 视频创建的结束时间，为UTC格式
        request.setEndTime(nowUTCTime);
        // 视频状态，默认获取所有状态的视频，多个用逗号分隔
        request.setStatus(status);
        request.setPageNo(1L);
        request.setPageSize(20L);
        return client.getAcsResponse(request);
    }
    /**
     * 获取视频上传地址和凭证函数
     *
     * @param title       标题
     * @param description 简介
     * @param fileName    文件名
     * @param tags        标签（默认一个）
     * @param coverURL    封面图片
     * @return
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(String title, String description, String fileName,
                                                              String tags, String coverURL) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setDescription(description);
        request.setFileName(fileName);
        request.setTags(tags);
        request.setCoverURL(coverURL);
        return client.getAcsResponse(request);
    }


    /**
     * 获取图片上传地址和凭证函数
     *
     * @param imageExt 图片后缀名
     * @return
     * @throws Exception
     */
    public static CreateUploadImageResponse createUploadImage(String imageExt) throws Exception {
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        request.setImageType("cover");
        request.setImageExt(imageExt);
        return client.getAcsResponse(request);
    }

    /*修改视频信息函数*/
    public static UpdateVideoInfoResponse updateVideoInfo(String id, String title, String description, String tags) throws Exception {
        UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();
        request.setVideoId(id);
        request.setTitle(title);
        request.setDescription(description);
        request.setTags(tags);
        return client.getAcsResponse(request);
    }

    /*删除视频函数*/
    public static DeleteVideoResponse deleteVideo(List<String> ids) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        StringBuilder deleteIds = new StringBuilder();
        for (String str : ids) {
            deleteIds.append(str + ",");
        }
        request.setVideoIds(deleteIds.toString());
        return client.getAcsResponse(request);
    }

    /*查询分类及其子分类函数*/
    public static GetCategoriesResponse getCategories(Long cateId, Long pageNo, Long pageSize) throws Exception {
        GetCategoriesRequest request = new GetCategoriesRequest();
        request.setCateId(cateId);
        request.setPageNo(pageNo);
        request.setPageSize(pageSize);
        return client.getAcsResponse(request);
    }


}
